﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PepsiCo.UI
{
    public partial class ChangePassword : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CurrentPassword.Focus();
            }
        }

        protected void CancelPushButton_Click(object sender, EventArgs e)
        {
            RedirectToPage(DefaultURL);
        }

        protected void ChangePasswordPushButton_Click(object sender, EventArgs e)
        {
            var curUser = DB.pepsi_Users.FirstOrDefault(p => p.UserID == CurUser.UserID);
            if (!curUser.Pwd.Equals(CurrentPassword.Text.Trim().ToMD5UTF8Encryption(), StringComparison.CurrentCultureIgnoreCase))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "alert('old password incorrectly!')", true);
                return;
            }
            else
            {
                curUser.Pwd = NewPassword.Text.Trim().ToMD5UTF8Encryption();
                DB.SubmitChanges();
                ReturnURL = DefaultURL;
                CurUser = null;
                RedirectToPage(LoginURL);
            }
        }
    }
}
